package com.ooqiu.gaming.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.ooqiu.gaming.GamingApp;
import com.ooqiu.gaming.injector.components.AppComponent;
import com.ooqiu.gaming.view.stateview.StateView;
import com.umeng.analytics.MobclickAgent;

import me.yokeyword.fragmentation.SupportFragment;


public abstract class BaseFragment extends SupportFragment implements StateView.OnRetryClickListener {

    private StateView mStateView;

    public abstract void initInjector(View view);

    public abstract int initContentView();


    public String title;

    protected Context mContext;


    /**
     * 初始化控件
     */
    public abstract void initUI(View view);

    /**
     * 在监听器之前把数据准备好
     */
    public abstract void initData();

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        ARouter.getInstance().inject(this);
        mContext = getContext();
        initUI(view);
        initData();
        super.onViewCreated(view, savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(initContentView(), container, false);
        initInjector(view);
        mStateView = initStatusLayout(view, statusView());
        mStateView.setOnRetryClickListener(this);
        return view;
    }



    private StateView initStatusLayout(View parent, View statue) {
        if (statue == null) {
            return StateView.inject(parent, hasActionBar());
        } else {
            return StateView.inject(statue);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    protected AppComponent getApplicationComponent() {
        return ((GamingApp) getActivity().getApplication()).getApplicationComponent();
    }


    protected View statusView() {
        return null;
    }


    protected void showLoading() {
        mStateView.showLoading();
    }

    protected void showContent() {
        mStateView.showContent();
    }

    protected void showEmpty() {
        mStateView.showEmpty();
    }

    protected void showRetry() {
        mStateView.showRetry();
    }

    protected void showRetry(String msg) {
        mStateView.showRetry(msg);
    }
    protected void showRetry(String msg,int id) {
        mStateView.showRetry(msg, id);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public ARouter getRouter() {
        return ARouter.getInstance();
    }


    @Override
    public void onRetryClick() {

    }


    public boolean hasActionBar() {
        return false;
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        MobclickAgent.onPageStart(getClass().getSimpleName() + pageExtra());
        MobclickAgent.onResume(getContext());
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        MobclickAgent.onPageEnd(getClass().getSimpleName() + pageExtra());
        MobclickAgent.onPause(getContext());
    }


    protected String pageExtra() {
        return "";
    }



}
